package com.neusoft.my12306.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.my12306.common.res.CommonResult;
import com.neusoft.my12306.entity.Roles;
import com.neusoft.my12306.entity.SysinfoRolesR;
import com.neusoft.my12306.mapper.RolesMapper;
import com.neusoft.my12306.mapper.SysinfoRolesRMapper;
import com.neusoft.my12306.service.IRolesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 段静迪
 * @since 2023-07-11
 */
@Service
public class RolesServiceImpl extends ServiceImpl<RolesMapper, Roles> implements IRolesService {
    @Autowired
    RolesMapper rolesMapper;

    @Autowired
    SysinfoRolesRMapper  sysinfoRolesRMapper;

    @Override
    public CommonResult findAll() {
        List<Roles> roles = rolesMapper.selectList(null);

        return new CommonResult(roles);
    }

    @Override
    public CommonResult modRoles(Roles roles) {
        int i =  rolesMapper.updateById(roles);
        return new CommonResult(i );
    }

    @Transactional // 启用业务层的事务
    @Override
    public CommonResult auth(String rid, String sysids) {

        String [] list =  sysids.split("-");
        QueryWrapper<SysinfoRolesR> wrapper = new QueryWrapper<>();
        wrapper.eq("rid" , rid);
        sysinfoRolesRMapper.delete(wrapper);

        for (int i = 0; i < list.length; i++) {
            sysinfoRolesRMapper.auth( rid , list[i] );
        }
        return new CommonResult();
    }
}
